package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.dao.MerGrpInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.MerGrpInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.service.MerGrpInfoService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author LiuYuLiang.LL
 */
@Service
@DS("upmp")
public class MerGrpInfoServiceImpl extends ServiceImpl<MerGrpInfoMapper, MerGrpInfo> implements MerGrpInfoService {
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Override
    public IPage<MerGrpInfo> findByPage(QueryRequest request, MerGrpInfo info) {
        try {
            LambdaQueryWrapper<MerGrpInfo> queryWrapper = new LambdaQueryWrapper<>();

            if (StringUtils.isNotBlank(info.getGrpNo())) {
                queryWrapper.like(MerGrpInfo::getGrpNo, info.getGrpNo());
            }
            if (StringUtils.isNotBlank(info.getGrpDesc())) {
                queryWrapper.like(MerGrpInfo::getGrpDesc, info.getGrpDesc());
            }
            queryWrapper.orderByDesc(MerGrpInfo::getCreateTime);
            Page<MerGrpInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
     public  IPage<MerGrpInfo>  mergrpinfoSelect2List(QueryRequest request, String q) {
        try {
            LambdaQueryWrapper<MerGrpInfo> queryWrapper = new LambdaQueryWrapper<>();
            q=q.replaceAll("&amp;", "&");

            String[] param = q.split("&");
            if (null != param && param.length > 0) {
                queryWrapper.or(qw -> qw.likeRight(MerGrpInfo::getGrpNo, param[0])
                        .likeRight(MerGrpInfo::getGrpDesc, param[0]));
            }
            queryWrapper.eq(MerGrpInfo::getStatus, BusinessContainer.data_sta_01);
            Page<MerGrpInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);

        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
    @Transactional
    public void create(MerGrpInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(),DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        entity.setStatus(BusinessContainer.data_sta_01);
        entity.setAuditStatus(BusinessContainer.status_02);
        entity.setOprId(user.getId());
        entity.setCrtOrgId(user.getOrgCode());
        entity.setCreateTime(lastUpddt);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);
        entity.setCardInstFlag("01");
        this.baseMapper.insert(entity);
    }
    @Override
    @Transactional
    public void update(MerGrpInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(),DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        // 修改
        entity.setAuditStatus(BusinessContainer.status_42);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);
        entity.setCardInstFlag("01");
        this.baseMapper.updateById(entity);
    }
    @Override
    @Transactional
    public void cancel(String id) {
        // 注销
        MerGrpInfo entity = this.getById(id);
        entity.setCardInstFlag("01");
        entity.setStatus(BusinessContainer.data_sta_03);
        entity.setAuditStatus(BusinessContainer.status_12);
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void delete(String[] ids) {
        List<String> list = Arrays.asList(ids);
        this.baseMapper.deleteBatchIds(list);
    }

    @Override
    public MerGrpInfo findOneByManagementScope(String managementScope, String status) {
        LambdaQueryWrapper<MerGrpInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerGrpInfo::getGrpNo, managementScope);
        queryWrapper.eq(MerGrpInfo::getStatus, status);
        return baseMapper.selectOne(queryWrapper);
    }
    @Override
    public MerGrpInfo findByGrpNo(String grpNo) {
        LambdaQueryWrapper<MerGrpInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerGrpInfo::getGrpNo, grpNo);
        return getOne(queryWrapper);
    }

    @Override
    public List<MerGrpInfo> findByGrpDesc(String grpDesc) {
        LambdaQueryWrapper<MerGrpInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerGrpInfo::getGrpDesc, grpDesc);
        return this.list(queryWrapper);
    }
    /**
     * 获取 所有的商户组别list
     */
    @Override
    public List<Map> getMcGrpList(String status) {

        List<Map> mcGrpInfnList = new ArrayList<>();
        LambdaQueryWrapper<MerGrpInfo> queryWrapper = new LambdaQueryWrapper<>();
        if(Common.isNotBlank(status)){
            queryWrapper.eq(MerGrpInfo::getStatus, status);
        }
        List<MerGrpInfo> merGrpInfos = this.list(queryWrapper);
        merGrpInfos.forEach((e) -> {
            Map map = new HashMap();
            map.put("GRPNO",e.getGrpNo());
            map.put("GRPDESC",e.getGrpDesc());
            mcGrpInfnList.add(map);
        });
        return mcGrpInfnList;
    }
}
